package mil.af.amc.adpetracker.web.actions.Account;

import mil.af.amc.adpetracker.service.AccountService;
import mil.af.amc.adpetracker.web.actions.AbstractAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Create an application login Account (as opposed to a User, which only implies someone who uses
 * Equipment Items.
 *
 * @author TSgt Jason Ferguson
 */

public class CreateAccountAction extends AbstractAction {

    private static final Log log = LogFactory.getLog(CreateAccountAction.class);

    private AccountService accountService;

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        DynaActionForm form = (DynaActionForm) actionForm;
        accountService.createAccount(form.getMap());
        log.info("Created account for username: " + form.get("email"));
        
//        try {
//        } catch (ServiceException e) {
//
//            ActionMessage message = new ActionMessage(e.getKey());
//            saveError(httpServletRequest, message);
//
//            return actionMapping.findForward("loginScreen");
//
//        }
//
//        ActionMessage message = new ActionMessage("messages.account.create");
//        saveError(httpServletRequest, message);

        return actionMapping.findForward("loginScreen");

    }
}
